﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;

namespace KPIS.GERP.GIMS.APP.APP_UI.Form
{
    public partial class ICM_Master_Resource : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
                RadTreeNode selected_node = RadTreeView1.SelectedNode;
                if (selected_node == null)
                {
                    ResourceTypeControl1.LoadDataGrid();
                }
            }
        }


        protected void RadTreeView1_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);
        }

        private static void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
        {
            IBLLTree bll = new TreeBLL();

            foreach (TreeInfo info in bll.GetResourceMasterTree(""))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                treeView.Nodes.Add(node);
            }
        }

        private static void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            e.Node.Dispose();
            e.Node.Nodes.Clear();
            IBLLTree bll = new TreeBLL();
            foreach (TreeInfo info in bll.GetResourceMasterTree(e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Expanded = true;
            e.Node.ExpandMode = expandMode;
        }

        public void PopulateNodeOnEditGrid(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            IBLLTree bll = new TreeBLL();
            foreach (TreeInfo info in bll.GetResourceMasterTree(e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }
            e.Node.Nodes.Clear();
            e.Node.ExpandMode = expandMode;
        }

        protected void btnExpand_Click(object sender, ImageClickEventArgs e)
        {
            IBLLTree bll = new TreeBLL();
            RadTreeView1.Nodes.Clear();
            RadTreeView1.DataSource = bll.GetResourceMasterTree();
            RadTreeView1.DataBind();
            RadTreeView1.ExpandAllNodes();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            IBLLTree bll = new TreeBLL();
            RadTreeView1.Nodes.Clear();
            if (txtSearch.Text.Length > 0)
            {
                RadTreeView1.DataSource = bll.GetResourceMasterTreeResultSearch(txtSearch.Text);
                RadTreeView1.DataBind();
                RadTreeView1.ExpandAllNodes();
                lblResult.Text = RadTreeView1.GetAllNodes().Count.ToString();
                PanelSearch.Visible = true;
            }
            else
            {
                LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
                PanelSearch.Visible = false;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            IBLLTree bll = new TreeBLL();
            RadTreeView1.Nodes.Clear();
            LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
            PanelSearch.Visible = false;
        }

        protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            RadTreeNode selected_node = RadTreeView1.SelectedNode;
            string value = selected_node.Value;
            string[] node_text = selected_node.Text.Split(new string[] { " - " }, StringSplitOptions.None);
            string[] all_value = value.Split('-');
            ResourceTypeControl1.Visible = false;
            ResourceGroupControl1.Visible = false;
            ResourceSubGroupControl1.Visible = false;
            ResourceItemControl1.Visible = false;
            BlankTreeControl1.Visible = false;

            if (all_value.Length == 1)
            {
                if (all_value[0] == "0")
                {
                    ResourceTypeControl1.LoadDataGrid();
                    ResourceTypeControl1.Visible = true;
                }
                else
                {
                    Session["TypeSeq"] = all_value[0];
                    ResourceGroupControl1.SetHeader(node_text[1]);
                    ResourceGroupControl1.LoadDataGrid();
                    ResourceGroupControl1.Visible = true;
                }
            }
            else if (all_value.Length == 2)
            {
                Session["GroupSeq"] = all_value[1];
                ResourceSubGroupControl1.SetHeader(node_text[1]);
                ResourceSubGroupControl1.LoadDataGrid();
                ResourceSubGroupControl1.Visible = true;
            }
            else if (all_value.Length == 3)
            {
                Session["SubGroupSeq"] = all_value[2];
                ResourceItemControl1.SetHeader(node_text[1]);
                ResourceItemControl1.LoadDataGrid();
                ResourceItemControl1.Visible = true;
            }
            else if (all_value.Length == 4)
            {
                BlankTreeControl1.Visible = true;
                BlankTreeControl1.SetHeader("การบันทึกโครงสร้างการจำแนกพัสดุ บันทึกได้ถึงระดับรายการพัสดุเท่านั้น");
            }
        }

        protected void btnCollapse_Click(object sender, EventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }

        protected void RadManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "CloseWindow")
            {
                if (ResourceTypeControl1.Visible)
                {
                    ResourceTypeControl1.LoadDataGrid();
                }
                if (ResourceGroupControl1.Visible)
                {
                    ResourceGroupControl1.LoadDataGrid();
                }
                if (ResourceSubGroupControl1.Visible)
                {
                    ResourceSubGroupControl1.LoadDataGrid();
                }
                if (ResourceItemControl1.Visible)
                {
                    ResourceItemControl1.LoadDataGrid();
                }
                RefreshTree();
            }
            else
            {
                if (ResourceTypeControl1.Visible)
                {
                    ResourceTypeControl1.GetKeyValue(e.Argument);
                }
                if (ResourceGroupControl1.Visible)
                {
                    ResourceGroupControl1.GetKeyValue(e.Argument);
                }
                if (ResourceSubGroupControl1.Visible)
                {
                    ResourceSubGroupControl1.GetKeyValue(e.Argument);
                }
                if (ResourceItemControl1.Visible)
                {
                    ResourceItemControl1.GetKeyValue(e.Argument);
                }
            }
        }

        public void RefreshTree()
        {
            RadTreeNode selected_node = RadTreeView1.SelectedNode;
            if (selected_node == null)
            {
                selected_node = RadTreeView1.FindNodeByValue("0");
            }
            RadTreeNodeEventArgs node = new RadTreeNodeEventArgs(selected_node);
            RadManager1.ResponseScripts.Add(@"CollapseSelectTree();");
            PopulateNodeOnEditGrid(node, TreeNodeExpandMode.ServerSideCallBack);
            RadManager1.ResponseScripts.Add(@"ExpandSelectTree();");
        }

        protected void OnAjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            RadAjaxPanel1.Controls.Add(Page.LoadControl("../UserControl/Personal_Data.ascx"));
        }
    }
}
